package com.jsits.v2x.compute.controller;

import com.jsits.v2x.common.dto.DetectDto;
import com.jsits.v2x.common.dto.ControlDto;
import com.jsits.v2x.common.enums.DevicePosition;
import com.jsits.v2x.compute.config.DataConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@Api(tags = "Compute", description = "车辆检测数据数据管理")
@Slf4j
@RequestMapping("/compute")
public class ComputeController {
    @Autowired
    private DataConfig dataConfig;

    @PostMapping("/pushdetect")
    @ApiOperation("车辆检测数据推送")
    public void pushDetectVehicleData(@RequestBody DetectDto detectVehicleDto) {
        Integer speed = detectVehicleDto.getSpeed();
        // 主路的数据需要进行过滤，只取20~199
        // 支路的数据只需要判断是否为0，来表明是否有行人
        if(detectVehicleDto.getDevicePosition().equals(DevicePosition.DEVICE_POSITION_MAIN_1)) {
            log.debug("main 1 speed " + speed);
            if(speed >= 200) speed = 199;
            if(speed < 20) speed = 0;
            dataConfig.setMain1Speed(speed);
        } else if(detectVehicleDto.getDevicePosition().equals(DevicePosition.DEVICE_POSITION_MAIN_2)) {
            log.debug("main 2 speed " + speed);
            if(speed >= 200) speed = 199;
            if(speed < 20) speed = 0;
            dataConfig.setMain2Speed(speed);
        } else if(detectVehicleDto.getDevicePosition().equals(DevicePosition.DEVICE_POSITION_SUB_1)) {
            log.debug("sub 1 speed " + speed);
            dataConfig.setSub1Speed(speed);
        } else if(detectVehicleDto.getDevicePosition().equals(DevicePosition.DEVICE_POSITION_SUB_2)) {
            log.debug("sub 2 speed " + speed);
            dataConfig.setSub2Speed(speed);
        }
    }

    @GetMapping("/getdisplay")
    @ApiOperation("获取各个屏幕显示的数据")
    public ArrayList<ControlDto> getDisplay() {
        ArrayList<ControlDto> controlDtoArrayList = new ArrayList<ControlDto>();
        if(dataConfig.getControlDtoMain1().getDevicePosition() != null) {
            controlDtoArrayList.add(dataConfig.getControlDtoMain1());
        }
        if(dataConfig.getControlDtoMain2().getDevicePosition() != null) {
            controlDtoArrayList.add(dataConfig.getControlDtoMain2());
        }
        if(dataConfig.getControlDtoSub1().getDevicePosition() != null) {
            controlDtoArrayList.add(dataConfig.getControlDtoSub1());
        }
        if(dataConfig.getControlDtoSub2().getDevicePosition() != null) {
            controlDtoArrayList.add(dataConfig.getControlDtoSub2());
        }
        return controlDtoArrayList;
    }
}
